package com.sunducation.wts.livedata2

import android.content.Context
import android.net.Uri
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.SeekBar
import androidx.lifecycle.*

class OneFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val parentView:View? = inflater.inflate(R.layout.fragment_one, container, false)

        parentView?.let {
            val seekBar: SeekBar = parentView.findViewById(R.id.seekBar)
            val shareDataViewModel: ShareDataViewModel = ViewModelProvider(requireActivity()).get(ShareDataViewModel::class.java)
            val liveData: LiveData<Int> = shareDataViewModel.getProgress();
            liveData.observe(requireActivity(), object: Observer<Int>{
                override fun onChanged(t: Int?) {
                    Log.i("OneFragment", "onChanged----${t}")
                    seekBar.setProgress(t?:0);
                }

            })
           seekBar.setOnSeekBarChangeListener(object: SeekBar.OnSeekBarChangeListener{
               override fun onProgressChanged(p0: SeekBar?, p1: Int, p2: Boolean) {
                   Log.i("OneFragment", "onProgressChanged----${p1}")
                   shareDataViewModel.setProgress(p1)
               }

               override fun onStartTrackingTouch(p0: SeekBar?) {
               }

               override fun onStopTrackingTouch(p0: SeekBar?) {
               }

           })
        }

        return parentView
    }

}
